import { queryStringify } from "@/utils/index.js";
let api = process.env.VUE_APP_BASEURL_API;
export default function requestPost(
  url,
  data,
  moduleName = "",
  method = "POST"
) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: api + moduleName + url,
      data: data,
      header: {
        "content-type": "application/json;charset=UTF-8",
        Authorization: "Bearer " + uni.getStorageSync("X-Token"),
      },
      method,
      dataType: "json",
      success: (res) => {
        resolve(res.data);
      },
      fail: (err) => {
        console.error("ssss", err);
        reject(err);
      },
      complete: (complete) => {
        //401todo
        console.log("complete=====", complete);
        if (complete.data?.code == 401) {
          status401();
        }
        resolve(complete.data);
      },
    });
  });
}
export function requestGet(url, data = {}, moduleName = "", show401Modal=true) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: api + moduleName + url,
      data: data,
      header: {
        "content-type": "application/x-www-form-urlencoded",
        Authorization: "Bearer " + uni.getStorageSync("X-Token"),
      },
      method: "GET",
      dataType: "json",
      success: (res) => {
        resolve(res.data);
      },
      fail: (err) => {
        console.log("requestGet====err======", err);
        reject(err);
      },
      complete: (complete) => {
        console.log("complete=====", complete);
        if (complete.data?.code == 401 && show401Modal) {
          status401();
        }
      },
    });
  });
}

export function requestDel(url, data = {}, moduleName = "", show401Modal=true) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: api + moduleName + url,
      data: data,
      header: {
        "content-type": "application/x-www-form-urlencoded",
        Authorization: "Bearer " + uni.getStorageSync("X-Token"),
      },
      method: "DELETE",
      dataType: "json",
      success: (res) => {
        resolve(res.data);
      },
      fail: (err) => {
        console.log("requestGet====err======", err);
        reject(err);
      },
      complete: (complete) => {
        console.log("complete=====", complete);
        if (complete.data?.code == 401 && show401Modal) {
          status401();
        }
      },
    });
  });
}

export function requestPut(
  url,
  data,
  moduleName = "",
  method = "PUT"
) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: api + moduleName + url,
      data: data,
      header: {
        "content-type": "application/json;charset=UTF-8",
        Authorization: "Bearer " + uni.getStorageSync("X-Token"),
      },
      method,
      dataType: "json",
      success: (res) => {
        resolve(res.data);
      },
      fail: (err) => {
        console.error("ssss", err);
        reject(err);
      },
      complete: (complete) => {
        //401todo
        console.log("complete=====", complete);
        if (complete.data?.code == 401) {
          status401();
        }
        resolve(complete.data);
      },
    });
  });
}
export function requestDelete(url, data = {}, moduleName = "") {
  return new Promise((resolve, reject) => {
    uni.request({
      url: api + moduleName + url,
      data: data,
      header: {
        "content-type": "application/x-www-form-urlencoded",
        Authorization: "Bearer " + uni.getStorageSync("X-Token"),
      },
      method: "DELETE",
      dataType: "json",
      success: (res) => {
        resolve(res.data);
      },
      fail: (err) => {
        // console.log(err);
        reject(err);
      },
      complete: (complete) => {
        console.log("complete=====", complete);
        if (complete.data?.code == 401) {
          status401();
        }
      },
    });
  });
}
function status401() {
  uni.showModal({
    title: "提示",
    content: "登录状态已过期，请重新登录",
    showCancel: false,
    success: function (res) {
      if (res.confirm) {
        uni.removeStorageSync("X-Token");
        uni.removeStorageSync("X-UserInfo");
        uni.navigateTo({
          url: "/pages/login/index",
        });
      } else if (res.cancel) {
        console.log("用户点击取消");
        uni.removeStorageSync("X-Token");
        uni.removeStorageSync("X-UserInfo");
      }
    },
  });
}
export function login(refresh = false) {
  console.log("login===========");
  uni.login({
    provider: "weixin",
    success(res) {
      console.log("login,weixin=============", res.code);
      requestGet(`/wxUser/wxLogin?code=${res.code}`, {}, "/baseplatform")
        .then((re) => {
          if (re.code === 200) {
            console.log("login index wx.login code = ", re);
            uni.setStorageSync("X-Token", re.data?.accessToken);
            uni.setStorageSync("X-UserInfo", re.data?.wuserExt);
            if (refresh) {
              // let pagesStack = getCurrentPages();
              // let pagesCurrent = pagesStack[pagesStack.length - 2];
              // console.log('重新打开上一个页面', pagesCurrent)
              // wx.reLaunch({
              //   url: `/${pagesCurrent.route}${queryStringify(pagesCurrent.options)}`
              // })
              uni.navigateBack();
            }
          } else {
            console.log("wxLogin失败了", re);
          }
        })
        .catch((error) => console.log("wxLogin失败了", error));
    },
    fail: (err) => {
      console.log("login fail======", err);
    },
  });
}
